package com.solidumgame.martillo.nail
{
	import Box2D.Collision.Shapes.b2MassData;
	import Box2D.Common.Math.b2Vec2;
	
	import com.pblabs.box2D.Box2DManagerComponent;
	import com.pblabs.box2D.Box2DSpatialComponent;
	import com.pblabs.box2D.PolygonCollisionShape;
	import com.pblabs.engine.PBE;
	import com.pblabs.engine.core.ObjectType;
	import com.solidumgame.martillo.hammer.Hammer;
	import com.solidumgame.martillo.tablewood.TableWood;
	
	import flash.geom.Point;
	
	public class NailSpatialComponent extends Box2DSpatialComponent
	{
		protected var _ratioX:Number;
		protected var _ratioY:Number;
		protected var nailSize:int;
		
		public function getLeftBasePoint():b2Vec2{
			
			var position:b2Vec2 = new b2Vec2(this.position.x-2,this.position.y+_ratioY);
			position.Multiply(this.spatialManager.inverseScale);
			position.y += this.body.GetLocalCenter().y;
			return position;
		}
		
		
		
		public function getRightBasePoint():b2Vec2{
			var position:b2Vec2 = new b2Vec2(this.position.x+2,this.position.y+_ratioY);
			position.Multiply(this.spatialManager.inverseScale);
			position.y += this.body.GetLocalCenter().y;
			return position;
		}
		
		public function NailSpatialComponent(position:Point, size:int)
		{
			super();
			
			
			this.spatialManager = PBE.lookupEntity("SpatialManager").lookupComponentByName("Manager") as Box2DManagerComponent;
			
			this.nailSize = size;
			this.position = position;
			
			this.canMove = true;
			this.canRotate = true;
			this.canSleep = true;
			this.collisionType = new ObjectType(Nail.TYPE);
			this.collidesWithTypes = new ObjectType(TableWood.TYPE);
			
			
			if(size == Nail.SIZE_SMALL)
				createSmallNail();
			else if(size == Nail.SIZE_MEDIUM)
				createMediumNail();
			else if(size == Nail.SIZE_LARGE)
				createLargeNail();	
			
		}
		
		private function createSmallNail():void{
			
			_ratioX = 7;
			_ratioY = 18.5;
			
			this.size = new Point(ratioX*2, ratioY*2);
			
			var nailHead:PolygonCollisionShape = getNailHead();
			var nailBody:PolygonCollisionShape = getNailBody();
			
			// Add the shape to spatial component.
			this.collisionShapes = new Array();
			this.collisionShapes.push(nailBody);
			this.collisionShapes.push(nailHead);
	
		}

		private function createMediumNail():void{
			
			_ratioX = 7;
			_ratioY = 27.5;
			
			this.size = new Point(ratioX*2, ratioY*2);
			

			var nailHead:PolygonCollisionShape = getNailHead();
			var nailBody:PolygonCollisionShape = getNailBody();
			
			// Add the shape to spatial component.
			this.collisionShapes = new Array();
			this.collisionShapes.push(nailBody);
			this.collisionShapes.push(nailHead);
			
			//this.collisionShapes.push(shape3);	
		}
		
		private function createLargeNail():void{
			_ratioX = 7;
			_ratioY = 36.5;
			
			this.size = new Point(ratioX*2, ratioY*2);
			
		
			var nailHead:PolygonCollisionShape = getNailHead();
			var nailBody:PolygonCollisionShape = getNailBody();
			
			// Add the shape to spatial component.
			this.collisionShapes = new Array();
			this.collisionShapes.push(nailBody);
			this.collisionShapes.push(nailHead);
		}

		protected function getNailBody():PolygonCollisionShape{
			
			// Body
			var nailBody:PolygonCollisionShape = new PolygonCollisionShape();
			nailBody.density = 1;
			nailBody.friction = 1;
			
			nailBody.vertices =  new Array();
			nailBody.vertices.push(new Point(-2/ratioX, -(ratioY-3)/ratioY));
			nailBody.vertices.push(new Point(2/ratioX, -(ratioY-3)/ratioY));
			nailBody.vertices.push(new Point(2/ratioX, (ratioY-8)/ratioY));
			nailBody.vertices.push(new Point(-2/ratioX, (ratioY-8)/ratioY));
			
			return nailBody;
			
			
		}		
		
		protected function getNailHead():PolygonCollisionShape{

			var nailHead:PolygonCollisionShape = new PolygonCollisionShape();
			
			nailHead.density = 1;
			nailHead.friction = 1;
			
			nailHead.vertices = new Array();
			nailHead.vertices.push(new Point(-7/ratioX, -1));
			nailHead.vertices.push(new Point(7/ratioX, -1));
			nailHead.vertices.push(new Point(7/ratioX, -(ratioY-3)/ratioY));
			nailHead.vertices.push(new Point(-7/ratioX, -(ratioY-3)/ratioY));
			
			return nailHead;
			
		}
		
		override protected function onReset():void{
			super.onReset();
			/*var mass:b2MassData = new b2MassData();
			mass.mass = 50;
			mass.I = 0.1;
			this.body.SetMass(mass);*/
		}

		public function get ratioX():Number
		{
			return _ratioX;
		}

		public function get ratioY():Number
		{
			return _ratioY;
		}

		public function set ratioY(value:Number):void
		{
			_ratioY = value;
		}


	}
}